package org.fastiot.data.sampling

import grails.converters.JSON
import grails.testing.gorm.DomainUnitTest
import org.grails.web.converters.configuration.ConvertersConfigurationHolder
import org.grails.web.converters.configuration.DefaultConverterConfiguration
import org.grails.web.converters.marshaller.json.CollectionMarshaller
import org.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller
import org.grails.web.converters.marshaller.json.MapMarshaller
import spock.lang.Specification

class TimingSpec extends Specification implements DomainUnitTest<Timing> {

    def setupSpec() {
        def defaultConverterConfig = new DefaultConverterConfiguration<JSON>()
        defaultConverterConfig.registerObjectMarshaller(new MapMarshaller())
        defaultConverterConfig.registerObjectMarshaller(new CollectionMarshaller())
        defaultConverterConfig.registerObjectMarshaller(new GenericJavaBeanMarshaller())

        ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, defaultConverterConfig)
    }

    def setup() {
    }

    def cleanup() {
    }

    void "Test a valid properties"() {
        given:
        def instance = new Timing(
                packetCode: packetCode, hexPacketCode: hexPacketCode, dateAcquired: dateAcquired,
                terminalCode: terminalCode, metadata: metadata, contents: contents
        )

        when:
        instance.save(flush: true)

        then:
        !instance.hasErrors()

        where:
        packetCode << [2304, null, 0x0900]
        hexPacketCode << [null, "0900", null]
        terminalCode << ["LN1234567890123456", "LN1234567890123456", "LN1234567890123456"]
        dateAcquired << [new Date(), new Date(), new Date()]
        metadata << [
                ([Oid: "LN1234567890123456", Pid: 0x0900] as JSON) as String,
                ([DateSent: "2018-02-14 12:13:14", "RemoteAddress": "10.11.12.13:23462"] as JSON) as String,
                ([SN: 1234, DateReceived: "2018-02-14 12:13:14"] as JSON) as String
        ]
        contents << [
                ([SOC: 80, MPH: 37.5] as JSON) as String,
                ([LAT: 39.5427, LON: 116.2317] as JSON) as String,
                ([TEMP: 80.3, VIB: 10.5] as JSON) as String
        ]
    }
}
